package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.NoteList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.NoteDetail;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.customview.GlideCircleTransform;
import com.lsy.weizhi2.myutils.ImageAdapterCall;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/15.
 */
public class HomeNotesAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<NoteList.ListBean> list_notes;
    private final int content = 1;
    private final int foote = 2;
    private final int head = 3;
    private int x, tag;
    private boolean bottom;
    private boolean ishead;

    //private NoteItemCall noteItemCall;
    public HomeNotesAdapter(Context context, List<NoteList.ListBean> list_notes) {
        this.context = context;
        this.list_notes = list_notes;
        layoutInflater = LayoutInflater.from(context);
    }

    public void UpData(List<NoteList.ListBean> list_notes, boolean bottom, int tag) {
        this.list_notes = list_notes;
        this.bottom = bottom;
        this.tag = tag;
        if (bottom) {
            x = 1;
        } else {
            x = 0;
            Log.i("Home_Hot", "x=0");
        }
        this.notifyDataSetChanged();
    }

    public void UpData(List<NoteList.ListBean> list_notes, boolean ishead){
        this.list_notes=list_notes;
        this.ishead=ishead;
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View head_view = layoutInflater.inflate(R.layout.head_view, parent, false);
        View noteitem = layoutInflater.inflate(R.layout.note, parent, false);
        View bottom_view = layoutInflater.inflate(R.layout.bottom_view, parent, false);
        AutoUtils.autoSize(head_view);
        AutoUtils.autoSize(noteitem);
        AutoUtils.autoSize(bottom_view);
        if (viewType == foote) {
            return new Bottom(bottom_view);
        } else if (viewType == content) {
            return new HomeNotesHolder(noteitem);
        } else if(viewType == head){
            return new Head(head_view);
        }else{
            return null;
        }
    }

    @Override
    public int getItemViewType(int position) {
        //此处不能用position=list_notes.size做标志
        if (bottom && position == list_notes.size()) {
            return foote;
        } else if (ishead && position == 0) {
            return head;
        } else {
            return content;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HomeNotesHolder) {
            int index = 0;
            if (ishead) {
                index = position - 1;
            } else {
                index = position;
            }
            ((HomeNotesHolder) holder).position = index;
            String[] str = null;
            if (list_notes != null && list_notes.size() != 0) {
                String photos = list_notes.get(index).getPhoto();
                if (photos != null && photos.length() != 0) {
                    str = photos.split(",");
                    ((HomeNotesHolder) holder).total_num.setText("共" + str.length + "张");
                }
                if (str != null) {
                    ((HomeNotesHolder) holder).imageAdapter.UpData(str);
                } else {
                    ((HomeNotesHolder) holder).image_rv.setVisibility(View.GONE);
                    ((HomeNotesHolder) holder).total_num.setVisibility(View.GONE);
                }
            }
            if (list_notes != null && list_notes.size() != 0) {
                ((HomeNotesHolder) holder).name_tv.setText(list_notes.get(index).getCreateUsername());
                ((HomeNotesHolder) holder).time_tv.setText(list_notes.get(index).getCreateTime());
                ((HomeNotesHolder) holder).title_tv.setText(list_notes.get(index).getTitle());
                int second = list_notes.get(index).getSecondCategoryId();
                ((HomeNotesHolder) holder).classify_tv.setText(Constants.name[second]);
                int method = list_notes.get(index).getPriceMethod();
                switch (method) {
                    case 0:
                        ((HomeNotesHolder) holder).price_tv.setText(list_notes.get(index).getPrice() + "/次");//服务单价
                        break;
                    case 1:
                        ((HomeNotesHolder) holder).price_tv.setText(list_notes.get(index).getPrice() + "/时");//服务单价
                        break;
                    case 2:
                        ((HomeNotesHolder) holder).price_tv.setText(list_notes.get(index).getPrice() + "/天");//服务单价
                        break;
                    case 3:
                        ((HomeNotesHolder) holder).price_tv.setText(list_notes.get(index).getPrice() + "/周");//服务单价
                        break;
                    case 4:
                        ((HomeNotesHolder) holder).price_tv.setText(list_notes.get(index).getPrice() + "/月");//服务单价
                        break;
                    case 5:
                        ((HomeNotesHolder) holder).price_tv.setText(list_notes.get(index).getPrice() + "/年");//服务单价
                        break;
                }
                ((HomeNotesHolder) holder).reply_number.setText(list_notes.get(index).getReplyCount() + "");
                ((HomeNotesHolder) holder).look_number.setText(list_notes.get(index).getClickCount() + "");
                ((HomeNotesHolder) holder).ok_number.setText(list_notes.get(index).getDealCount() + "");
            }
            Glide.with(context).load(new GlideUrl(Constants.DOWN_USER_PICTURE + list_notes.get(index).getAvatar(), new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID", MyInfo.User_Id + "").build()))
                    .transform(new GlideCircleTransform(context))
                    .error(R.mipmap.default_head)
                    .into(((HomeNotesHolder) holder).head_image);
        } else if (holder instanceof Bottom) {
            if (tag == 1) {
                ((Bottom) holder).bottom_tv.setText("松开加载");
            } else if (tag == 2) {
                ((Bottom) holder).bottom_tv.setText("正在加载");
            } else if (tag == 3) {
                ((Bottom) holder).bottom_tv.setText("到底了");
            }
        }else if(holder instanceof Head){

        }


    }

    @Override
    public int getItemCount() {
        return list_notes == null ? 0 : list_notes.size() + x;
    }

    class HomeNotesHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        int position;
        private RelativeLayout note_item_view;
        private TextView name_tv, time_tv, hot_tv, classify_tv, follow_tv, title_tv, follow_number, reply_number, look_number, price_tv, total_num, ok_number;
        private ImageView head_image;
        private RecyclerView image_rv;
        private ImageAdapter imageAdapter;

        public HomeNotesHolder(View itemView) {
            super(itemView);
            note_item_view = (RelativeLayout) itemView.findViewById(R.id.note_item_view);
            ok_number = (TextView) itemView.findViewById(R.id.ok_number);
            name_tv = (TextView) itemView.findViewById(R.id.name_tv);
            time_tv = (TextView) itemView.findViewById(R.id.time_tv);
            hot_tv = (TextView) itemView.findViewById(R.id.hot_tv);
            classify_tv = (TextView) itemView.findViewById(R.id.classify_tv);
            follow_tv = (TextView) itemView.findViewById(R.id.follow_tv);
            title_tv = (TextView) itemView.findViewById(R.id.title_tv);
            follow_number = (TextView) itemView.findViewById(R.id.follow_number);
            reply_number = (TextView) itemView.findViewById(R.id.reply_number);
            look_number = (TextView) itemView.findViewById(R.id.look_number);
            price_tv = (TextView) itemView.findViewById(R.id.price_tv);
            total_num = (TextView) itemView.findViewById(R.id.total_num);
            head_image = (ImageView) itemView.findViewById(R.id.head_image);
            image_rv = (RecyclerView) itemView.findViewById(R.id.image_rv);
            LinearLayoutManager manager = new LinearLayoutManager(context);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            image_rv.setLayoutManager(manager);
            imageAdapter = new ImageAdapter(context, null);
            image_rv.setAdapter(imageAdapter);
            note_item_view.setOnClickListener(this);
            follow_tv.setOnClickListener(this);
            imageAdapter.setImageAdapterCall(new ImageAdapterCall() {
                @Override
                public void onClick(View view, int x) {
                    Gson gson = new Gson();
                    String json = gson.toJson(list_notes.get(position));
                    MyMethod.toActivity(context, NoteDetail.class, new String[]{"json"}, new String[]{json});
                }

                @Override
                public void onDetele(View view, int x) {

                }
            });
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.follow_tv:
                    //调用服务器接口--关注某人
                    follow_tv.setText("已关注");
                    break;
                case R.id.note_item_view:
                    //触发点击帖子事件
                    Gson gson = new Gson();
                    String json = gson.toJson(list_notes.get(position));
                    MyMethod.toActivity(context, NoteDetail.class, new String[]{"json"}, new String[]{json});
                    break;
            }
        }
    }

    class Bottom extends RecyclerView.ViewHolder {
        TextView bottom_tv;

        public Bottom(View itemView) {
            super(itemView);
            bottom_tv = (TextView) itemView.findViewById(R.id.bottom_tv);
        }
    }

    class Head extends RecyclerView.ViewHolder {
        TextView head_tv;
        ImageView head_iv;

        public Head(View itemView) {
            super(itemView);
            head_iv = (ImageView) itemView.findViewById(R.id.head_iv);
            head_tv = (TextView) itemView.findViewById(R.id.head_tv);
        }
    }
}
